package com.admission.web.admin;

import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.model.Special;
import com.admission.model.SpecialArticle;
import com.admission.service.SpecialArticleManager;
import com.admission.service.SpecialManager;
import com.admission.util.PageUtils;
import com.admission.web.CrudActionSupport;
@Namespace("/admin")
public class SpecialArticleAdminAction extends CrudActionSupport<SpecialArticle>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private SpecialArticle entity;
	private SpecialArticleManager specialArticleManager;
	private SpecialManager specialManager;
	private String id;
	private String ids;
	private Page<SpecialArticle> page=new Page<SpecialArticle>(PageUtils.DEFAULT_PAGE_SIZE);
	private String spec_id;
	private List<Special> specialList;
	private Special special;
	private String type;
	
	public SpecialArticle getModel() {
		return entity;
	}

	@Override
	public String list() throws Exception {
		specialList=specialManager.getAll();
		special=specialManager.getSpecialById(spec_id);
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		PropertyFilter p=new PropertyFilter("EQS_special.id",spec_id);
		filters.add(p);
		page.setOrder(Page.DESC);
		page.setOrderBy("inputTime");
		page=specialArticleManager.getPageByFilter(page, filters);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		special=specialManager.getSpecialById(spec_id);
		specialList=specialManager.getAll();
		return INPUT;
	}
	
	
	public String recommend() throws Exception{
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		PropertyFilter p=new PropertyFilter("EQS_tag",SpecialArticle.TAGTRUE);
		filters.add(p);
		page.setOrder(Page.DESC);
		page.setOrderBy("inputTime");
		page=specialArticleManager.getPageByFilter(page, filters);
		return "recommend";
	}
	


	@Override
	public String save() throws Exception {
		try{
			if(!StringUtils.isEmpty(spec_id))
				entity.setSpecial(specialManager.getSpecialById(spec_id));
			specialArticleManager.save(entity);
		} catch (Exception e) {
		   this.addActionMessage("操作失败，请重新操作");
	    }
		if(type.equals("0"))
			return list();
		else
			return recommend();
	}

	public String recdelete() throws Exception{
		specialArticleManager.deleteByIds(ids);
		return recommend();
	}
	
	@Override
	public String delete() throws Exception {
			specialArticleManager.deleteByIds(ids);
				return list();
	}
	
	
	@Override
	protected void prepareModel() throws Exception {
		if(StringUtils.isEmpty(id)){
			entity = new SpecialArticle();
		}else{
			entity = specialArticleManager.getById(id);
		}
		
	}

	public SpecialArticle getEntity() {
		return entity;
	}

	public void setEntity(SpecialArticle entity) {
		this.entity = entity;
	}

	public SpecialArticleManager getSpecialArticleManager() {
		return specialArticleManager;
	}
	@Autowired
	public void setSpecialArticleManager(SpecialArticleManager specialArticleManager) {
		this.specialArticleManager = specialArticleManager;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public Page<SpecialArticle> getPage() {
		return page;
	}

	public void setPage(Page<SpecialArticle> page) {
		this.page = page;
	}

	public String getSpec_id() {
		return spec_id;
	}

	public void setSpec_id(String spec_id) {
		this.spec_id = spec_id;
	}


	public SpecialManager getSpecialManager() {
		return specialManager;
	}
	@Autowired
	public void setSpecialManager(SpecialManager specialManager) {
		this.specialManager = specialManager;
	}

	public List<Special> getSpecialList() {
		return specialList;
	}

	public void setSpecialList(List<Special> specialList) {
		this.specialList = specialList;
	}

	public Special getSpecial() {
		return special;
	}

	public void setSpecial(Special special) {
		this.special = special;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	

}
